package routes

import (
	"saysay/handlers"
	"saysay/middleware"

	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
)

// SetupRoutes 配置路由
func SetupRoutes(router *gin.Engine, secretKey string) {
	// 配置会话
	store := cookie.NewStore([]byte(secretKey))
	router.Use(sessions.Sessions("saysay_session", store))

	// 首页
	router.GET("/", handlers.IndexHandler)

	// 用户路由
	router.GET("/register", handlers.RegisterPageHandler)
	router.POST("/register", handlers.RegisterHandler)
	router.GET("/login", handlers.LoginPageHandler)
	router.POST("/login", handlers.LoginHandler)
	router.GET("/logout", handlers.LogoutHandler)

	// API路由
	router.POST("/api/post", handlers.PostShuoshuoHandler)
	router.POST("/api/delete", handlers.DeleteShuoshuoHandler)
	router.POST("/api/comment", handlers.PostCommentHandler)
	router.GET("/api/comments", handlers.GetCommentsHandler)

	// 管理员路由
	admin := router.Group("/admin")
	{

		admin.GET("/login", handlers.AdminLoginPageHandler)
		admin.POST("/login", handlers.AdminLoginHandler)
		admin.GET("/logout", handlers.AdminLogoutHandler)

		// 需要认证的管理后台
		admin.Use(middleware.AuthRequired())
		{
			admin.GET("/", handlers.AdminHandler)
			admin.POST("/toggle-violation", handlers.ToggleViolationHandler)
			admin.POST("/delete-shuoshuo", handlers.AdminDeleteShuoshuoHandler)

			// 用户管理
			admin.GET("/users", handlers.AdminUsersHandler)
			admin.POST("/ban-user", handlers.AdminBanUserHandler)
			admin.POST("/delete-user", handlers.AdminDeleteUserHandler)
		}
	}
}
